﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using WebApplication1;

namespace WebApplication1.Controllers
{
    /*
    The WebApiConfig class may require additional changes to add a route for this controller. Merge these statements into the Register method of the WebApiConfig class as applicable. Note that OData URLs are case sensitive.

    using System.Web.Http.OData.Builder;
    using System.Web.Http.OData.Extensions;
    using WebApplication1;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Hot6>("Hot6");
    builder.EntitySet<ProductDB>("ProductDBs"); 
    config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    */
    public class Hot6Controller : ODataController
    {
        private DB db = new DB();

        // GET: odata/Hot6
        [EnableQuery]
        public IQueryable<Hot6> GetHot6()
        {
            return db.Hot6;
        }

        // GET: odata/Hot6(5)
        [EnableQuery]
        public SingleResult<Hot6> GetHot6([FromODataUri] int key)
        {
            return SingleResult.Create(db.Hot6.Where(hot6 => hot6.Hot_Id == key));
        }

        // PUT: odata/Hot6(5)
        public async Task<IHttpActionResult> Put([FromODataUri] int key, Delta<Hot6> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Hot6 hot6 = await db.Hot6.FindAsync(key);
            if (hot6 == null)
            {
                return NotFound();
            }

            patch.Put(hot6);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!Hot6Exists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(hot6);
        }

        // POST: odata/Hot6
        public async Task<IHttpActionResult> Post(Hot6 hot6)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Hot6.Add(hot6);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (Hot6Exists(hot6.Hot_Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return Created(hot6);
        }

        // PATCH: odata/Hot6(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Hot6> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Hot6 hot6 = await db.Hot6.FindAsync(key);
            if (hot6 == null)
            {
                return NotFound();
            }

            patch.Patch(hot6);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!Hot6Exists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(hot6);
        }

        // DELETE: odata/Hot6(5)
        public async Task<IHttpActionResult> Delete([FromODataUri] int key)
        {
            Hot6 hot6 = await db.Hot6.FindAsync(key);
            if (hot6 == null)
            {
                return NotFound();
            }

            db.Hot6.Remove(hot6);
            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }

        // GET: odata/Hot6(5)/ProductDB
        [EnableQuery]
        public SingleResult<ProductDB> GetProductDB([FromODataUri] int key)
        {
            return SingleResult.Create(db.Hot6.Where(m => m.Hot_Id == key).Select(m => m.ProductDB));
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool Hot6Exists(int key)
        {
            return db.Hot6.Count(e => e.Hot_Id == key) > 0;
        }
    }
}
